﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UIT.Controls;
using UIT.Markup;
using UIT.Web.Renderers.Specials;
using UIT.Web.Controls.Template;

namespace UIT.Web.Renderers
{
	public abstract class WebControlRenderer<T> : ControlRenderer<T>, IWebRenderer
		where T : class, IControl
	{
		protected WebControlRenderer()
		{
		}

		public RunAtMode RunAt { get; set; }

		protected XmlWriter writer;

		public override void Initialize(RendererManager manager, object userObject)
		{
			base.Initialize(manager, userObject);
			this.writer = userObject as XmlWriter;
		}

		protected void Render(IControl control, string propertyName, string valueAttribute)
		{
			Binding binding = control.GetBinding(propertyName);
			if (binding != null && binding.Mode != BindingMode.OneTime && RunAt == RunAtMode.Client)
				binding.Render(writer);
			else
				writer.WriteAttributeString(valueAttribute, new BindingFragment(false) { Member = propertyName }.GetValue(control).ToString());
		}
	}
}
